//
//  TSCameraModule.m
//  dsBridge
//
//  Created by Zac on 2020/10/28.
//

#import "TSCameraModule.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface TSCameraModule ()
@property (nonatomic, copy)void (^completionBlock)(UIImage *image);
@end

@implementation TSCameraModule

/// 获取相机选定照片
/// @param viewController 指定控制器
/// @param sourceType 相机or相册
/// @param allowsEditing 是否允许编辑
/// @param completionBlock <#completionBlock description#>
+ (void)pickImageWithController:(id)viewController
                     sourceType:(UIImagePickerControllerSourceType)sourceType
                  allowsEditing:(BOOL)allowsEditing
                completionBlock:(void(^)(UIImage *image))completionBlock
{
    if (sourceType == UIImagePickerControllerSourceTypeCamera&&![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"该设备不支持相机调用哦!");
        return;
    }
    TSCameraModule *imagePicker = [[TSCameraModule alloc]init];
    imagePicker.completionBlock = completionBlock;
    [[viewController rac_signalForSelector:@selector(imagePickerController:didFinishPickingImage:editingInfo:) fromProtocol:@protocol(UIImagePickerControllerDelegate)] subscribeNext:^(id x) {
        if (imagePicker.completionBlock) {
            imagePicker.completionBlock([x objectAtIndex:1]);
        }
        [imagePicker dismissViewControllerAnimated:YES completion:^{
            imagePicker.completionBlock = nil;
            [imagePicker removeFromParentViewController];
        }];
    }];
    imagePicker.allowsEditing = NO;
    imagePicker.delegate = viewController;
    imagePicker.sourceType = sourceType;

    [viewController presentViewController:imagePicker animated:YES completion:nil];

}

@end
